﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using NetCoreCMS.Common;
using NetCoreCMS.IServices;
using NetCoreCMS.Models;
using Newtonsoft.Json.Linq;

namespace NetCoreCMS.Web.Areas.Admin.Controllers
{
    public class Nav_ListController : ManageController
    {
        private const string WEB_VIEW = "~/Areas/Admin/Views/Settings/nav_list.cshtml";
        public INavigationService _navigation { get; set; }
        public override void OnActionExecuting(ActionExecutingContext context)
        {
            base.OnActionExecuting(context);
            if (context.Result != null)
                return;
            ChkAdminLevel("sys_navigation", CMSEnums.ActionEnum.View.ToString()); //检查权限
        }
        // GET: /admin/Nav_List/

        public ActionResult Index()
        {
            RptBind();
            return View(WEB_VIEW);
        }

        [HttpPost]
        public ActionResult SubmitSave()
        {
            ChkAdminLevel("sys_navigation", CMSEnums.ActionEnum.Edit.ToString()); //检查权限
            JObject jobject = JObject.Parse(Request.Form["json"]);
            JToken list = jobject["list"];
            foreach (JToken item in list)
            {
                int id = int.Parse(item["id"].ToString());
                int sortId = int.Parse(item["sortId"].ToString());
                _navigation.UpdateField(id, "sort_id=" + sortId);
            }
            AddAdminLog(CMSEnums.ActionEnum.Edit.ToString(), "保存导航排序"); //记录日志
            string script = JscriptMsg("保存排序成功！", "index");
            return Content(script);
        }

        [HttpPost]
        public ActionResult SubmitDelete()
        {
            ChkAdminLevel("sys_navigation", CMSEnums.ActionEnum.Delete.ToString()); //检查权限
            JObject jobject = JObject.Parse(Request.Form["json"]);
            JToken list = jobject["list"];
            foreach (JToken item in list)
            {
                int id = int.Parse(item["id"].ToString());
                _navigation.Delete(id);
            }
            AddAdminLog(CMSEnums.ActionEnum.Delete.ToString(), "删除导航菜单"); //记录日志
            string script = JscriptMsg("删除数据成功！", "index", "parent.loadMenuTree");
            return Content(script);
        }

        //数据绑定
        private void RptBind()
        {
            ViewData["list"] = _navigation.GetList(0, CMSEnums.NavigationEnum.System.ToString()); ;
        }
    }
}
